package org.will.test.util;

import com.alibaba.fastjson.JSONObject;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by jsycwangwei on 2017/7/9.
 */
public class XML {
    public static JSONObject toJsonObject(String content) {
        if (null == content || "".equals(content)) {
            return null;
        }
        try (InputStream in = new ByteArrayInputStream(content.getBytes("UTF-8"))) {
            return (JSONObject) inputStream2Map(in,null);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Map inputStream2Map(InputStream in, Map m) throws IOException {
        if (null == m) {
            m = new JSONObject();
        }
        SAXBuilder builder = new SAXBuilder();
        try {
            Document doc = builder.build(in);
            Element root = doc.getRootElement();
            List list = root.getChildren();
            Iterator it = list.iterator();
            while (it.hasNext()){
                Element element = (Element)it.next();
                String k = element.getName();
                String v = "";
                List children = element.getChildren();
                if (children.isEmpty()){
                    v = element.getTextNormalize();
                }else{
                    v = getChildrenText(list);
                }
                m.put(k,v);
            }
        } catch (JDOMException e) {
            e.printStackTrace();
        }
        return m;
    }

    public static String getChildrenText(List children){
        StringBuilder sb = new StringBuilder();
        if (!children.isEmpty()){
            Iterator it = children.iterator();
            while (it.hasNext()){
                Element element = (Element)it.next();
                String name = element.getName();
                String value = element.getValue();
                List list = element.getChildren();
                sb.append("<").append(name).append(">");
                if (!list.isEmpty()){
                    sb.append(getChildrenText(list));
                }
                sb.append(value);
                sb.append("</").append(name).append(">");
            }
        }
        return sb.toString();
    }

    public static String getMap2Xml(Map<String, Object> parameters) {
        StringBuffer sb = new StringBuffer();
        sb.append("<xml>");
        for (String key : parameters.keySet()){
            if ("attach".equalsIgnoreCase(key) || "body".equalsIgnoreCase(key) || "sign".equalsIgnoreCase(key)) {
                sb.append("<" + key + ">" + "<![CDATA[" + parameters.get(key) + "]]></" + key + ">");
            } else {
                sb.append("<" + key + ">" +  parameters.get(key) + "</" + key + ">");
            }

        }

        sb.append("</xml>");
        return sb.toString();
    }
}
